//
//  THJAllClassView.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/7.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJAllClassView.h"
#import "THJAllClassCell.h"
#import "THJAllClassReusableView.h"

@implementation THJAllClassView
{
    UITableView * _tableView;
    UICollectionView * _collectionView;
    NSInteger selectIndex;
    NSString * midType;
}

-(instancetype)initWithMid:(NSString*)mid{
    if(self = [super init]){
        midType = mid;
        [self setUI];
    }
    return self;
}
//-(instancetype)initWithFrame:(CGRect)frame{
//    if(self = [super initWithFrame:frame]){
////        midType = mid;
//        [self setUI];
//    }
//    return self;
//}
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 设置collectionView的滚动方向，需要注意的是如果使用了collectionview的headerview或者footerview的话， 如果设置了水平滚动方向的话，那么就只有宽度起作用了了
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
         layout.minimumInteritemSpacing = 5;// 垂直方向的间距
         layout.minimumLineSpacing = 5; // 水平方向的间距
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(ScreenWidth/4+10, 0, ScreenWidth/4*3, ScreenHeight-64) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerNib:[UINib nibWithNibName:@"THJAllClassCell" bundle:nil] forCellWithReuseIdentifier:@"CVcell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"THJAllClassReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}
-(void)setUI{
    selectIndex = 0;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth/4+10, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [UIView new];
    [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self addSubview:_tableView];
    [self addSubview:self.collectionView];
    [self getData];
}
#pragma mark --- tableviewDelegate ----
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
   
    if(indexPath.row == selectIndex){
        cell.backgroundColor = [UIColor whiteColor];
         [cell.textLabel setTextColor:MainColor];
    }else{
        cell.backgroundColor = COLOR(237, 237, 237, 1);
        [cell.textLabel setTextColor:COLOR(38, 38, 38, 1)];
    }
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    NSDictionary * dic = self.dataArr[indexPath.row];
    cell.textLabel.text = dic[@"catname"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    selectIndex = indexPath.row;
    [tableView reloadData];
    [_collectionView reloadData];
//    NSIndexPath * index = [NSIndexPath indexPathForRow:1 inSection:indexPath.row];
//    [self.collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
}
- (void)getData {
    //    __weak typeof(self) weakself = self;

    NSString * urlStr = [NSString stringWithFormat:@"%@index.php?f=Category&r=Category&mid=%@",THJUrlDomain,midType];
    NSLog(@"产品分类赛选url-%@",urlStr);
    [THJNetWork GetDataWithGetRequestTypeUrl:urlStr Callback:^(NSDictionary* receiveData, NSError *error) {
        self.dataArr = receiveData[@"data"];
        [_tableView reloadData];
        [_collectionView reloadData];
    }];
    
}
#pragma mark --- collecitonViewDelegate ---
/** 头部/底部*/
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    NSArray * arr = self.dataArr[selectIndex][@"data"];
    NSDictionary * dic = arr[indexPath.section];
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 头部
        THJAllClassReusableView * view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        view.textLab.text = dic[@"catname"];
        return view;
    }
    return nil;
}
/** 头部的尺寸*/
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(ScreenWidth, 40);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSArray * arr = self.dataArr[selectIndex][@"data"];
    NSLog(@"colview,oucnt-%ld",arr.count);
    return arr.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSDictionary * dic = self.dataArr[selectIndex][@"data"][section];
    NSArray * arr = dic[@"data"];
    return arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic1 = self.dataArr[selectIndex][@"data"][indexPath.section];
    NSArray * arr  = dic1[@"data"];
    NSDictionary * dic2 = arr[indexPath.row];
    THJAllClassCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVcell" forIndexPath:indexPath];
    cell.textLab.text = dic2[@"catname"];
    NSLog(@"colviewdic-%@",dic2);
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((ScreenWidth/4*3 - 30)/3, 60);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic1 = self.dataArr[selectIndex][@"data"][indexPath.section];
    NSArray * arr  = dic1[@"data"];
    NSDictionary * dic2 = arr[indexPath.row];
    
    NSLog(@"%@-%@", dic2[@"catname"],dic2[@"catid"]);
    [self.delegate selectedCatName:dic2[@"catname"] CatId:dic2[@"catid"]];
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 10, 5, 10);
}

@end
